package com.example.expandablelistview.parser;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.example.expandablelistview.R;

public class Pull_parserAcitivity extends Activity {
	ListView list;
	ProgressDialog progressDlg;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.network_xmlparser);
		list = (ListView) findViewById(R.id.listView);
		new ParsingXml().execute("order.xml");
	}

	private class ParsingXml extends AsyncTask<String, Void, ArrayList<Item>> {
		protected void onPreExecute() {
			progressDlg = ProgressDialog.show(Pull_parserAcitivity.this,
					"Wait", "order.xml & order.json Making ...");
		}

		protected void onPostExecute(ArrayList<Item> result) {
			MyAdapter adapter = new MyAdapter(Pull_parserAcitivity.this,
					R.layout.itemrow, result);
			progressDlg.dismiss();
			list.setAdapter(adapter);
		}

		protected ArrayList<Item> doInBackground(String... params) {
			StringBuffer sb = new StringBuffer();
			String str = null;
			try {
				BufferedReader fis = new BufferedReader(new InputStreamReader(
						openFileInput(params[0])));
				while ((str = fis.readLine()) != null) {
					sb.append(str);
				}
				fis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return parsingXml(sb.toString());
		}
	}

	public ArrayList<Item> parsingXml(String xml) {
		ArrayList<Item> arItem = new ArrayList<Item>();
		Item itemClass = null;
		boolean initem = false;

		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(new StringReader(xml));

			int eventType = parser.getEventType();

			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
					case XmlPullParser.START_DOCUMENT:
						break;
					case XmlPullParser.END_DOCUMENT:
						break;
					case XmlPullParser.TEXT:
						if (initem) {
							itemClass.setItemName(parser.getText());
							initem = false;
						}
						break;
					case XmlPullParser.START_TAG:
						if (parser.getName().equals("item")) {
							initem = true;
							itemClass = new Item();
						}
						for (int i = 0; i < parser.getAttributeCount(); i++) {
							if (parser.getAttributeName(i).equalsIgnoreCase(
									"price")) {
								itemClass.setItemPrice(Integer.parseInt(parser
										.getAttributeValue(i)));
							} else {
								itemClass.setMakerName(parser
										.getAttributeValue(i));
							}
						}
						break;
					case XmlPullParser.END_TAG:
						if (parser.getName().equals("item")) {
							arItem.add(itemClass);
						}
						break;
				}// end of switch
				eventType = parser.next();
			}// end of while
		} catch (Exception e) {
			Log.i("Pull_Parser", e.getMessage());
		}
		return arItem;
	}// parsingXml();
}
